<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新增角色</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <link rel="stylesheet" href="css/eleTree.css">
    <style>
        .layui-form-label{
            width: 110px;
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <blockquote class="layui-elem-quote">
            新增角色
            <button type="button" class="layui-btn layui-btn-sm" style="float: right;margin-top: -4px;" onclick="window.history.back();">返回</button>
        </blockquote>
        <form class="layui-form" action="" lay-filter="example">
            <div class="layui-form-item">
              <label class="layui-form-label">角色名称：</label>
              <div class="layui-input-inline">
                <input type="text" name="roleName" autocomplete="off" placeholder="请输入角色名称" class="layui-input" maxlength="20">
              </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态：</label>
                <div class="layui-input-block">
                    <input type="radio" name="openStatus" value="1" title="启用" checked="">
                    <input type="radio" name="openStatus" value="0" title="禁用">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色权限：</label>
                <div class="layui-input-inline">
                    <div class="eleTree ele1"></div>
                </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">确认</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="window.history.back()">取消</button>
              </div>
            </div>
        </form>
    </div>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>

    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'form', 'eleTree'], function() {
        var http = layui.http,
            form = layui.form,
            eleTree = layui.eleTree;

            
        //用户权限
        var el = '';
        getPermission();
        function getPermission(){
            http.get('supplier/admin/role/listAllAuth',{},function(res){
                if(res.code == 200){
                    var data = res.data;
                    // for(var i=0;i<data.length;i++){
                    //     if(data[i].children){
                    //         for(var j=0;j<data[i].children.length;j++){
                    //             data[i].children[j].disabled = true;

                    //         }
                    //     }
                    // }
                    el = eleTree.render({
                        elem: '.ele1',
                        data: data,
                        showCheckbox: true,
                        defaultExpandAll:true,
                        request: {     // 对后台返回的数据格式重新定义
                            name: "title",
                            key: "id",
                            children: "children",
                            checked: "checked",
                            disabled: "disabled",
                            isLeaf: "isLeaf"
                        }
                    });
                }
            });
        }
        
        //监听提交
        form.on('submit(demo1)', function(data){
            // console.log(data.field)
            if($.trim(data.field.roleName) == "") {
                layer.msg('请输入角色名称')
                return
            }
            
            var checkedList = el.getChecked(false, true);
            var permissionIds = [];
            if(checkedList.length == 0){
                layer.msg('请选择角色权限');
                return;
            }
            for(var i=0;i<checkedList.length;i++){
                permissionIds.push(checkedList[i].id);
            };
            http.postJSON('supplier/admin/role/add',JSON.stringify({
                roleName: data.field.roleName,
                openStatus: data.field.openStatus,
                authIdList: permissionIds
            }),function(res){
                if(res.code == 200){
                    layer.msg('操作成功')
                    setTimeout(function(){
                        window.history.back()
                    },500)
                }else{
                    layer.msg(res.msg)
                }
            })
        });
    })
</script>
</body>